﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;
using Gem_Tools_Utility;
using DevExpress.XtraEditors;
using Gem_Tools;

namespace GE_Management_Tool.Home_Page.UserControl
{
    public partial class uc1004_To_Do_List : DevExpress.XtraEditors.XtraUserControl
    {
        CAppointments m_objSelectedAppointment;
        int m_iCountTime = 30;
        IList<CAppointments> m_lsAppointment;
        IList<CAsset> m_lsAsset;

        public uc1004_To_Do_List()
        {
            InitializeComponent();
            if (CSession.Active_Staff == null)
            {
                throw new Exception("Network Error !");
            }
            CCommon_Function.Format_All(this);
            this.grvData.OptionsBehavior.Editable = false;
        }

        private void Init_Data()
        {
            m_lsAppointment = new List<CAppointments>();
            CAppointments_Controller v_objCtrlTasks = new CAppointments_Controller();
            m_lsAppointment = v_objCtrlTasks.A1004_sp_sel_List_Appointments_By_Staff_ID(CSession.Active_Staff.Auto_ID);

            grdData.DataSource = m_lsAppointment;
        }

        private void Load_Task_Asset(int piTask_ID)
        {
            m_lsAsset = new List<CAsset>();
            CAsset_Controller v_CtrlAsset = new CAsset_Controller();
            m_lsAsset = v_CtrlAsset.A1004_sp_sel_List_Tasks_By_Task_ID(piTask_ID);
        }

        private void uc1004_To_Do_List_Load(object sender, EventArgs e)
        {
            try
            {
                btnRefresh_Click(null, null);
                IList<CProject> v_lsProject = new List<CProject>();
                CProject_Controller v_CtrlProject = new CProject_Controller();
                v_lsProject = v_CtrlProject.List_Project();
                lkeProject.DataSource = v_lsProject;
                TimerRefreshData.Start();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void TimerRefreshData_Tick(object sender, EventArgs e)
        {
            try
            {
                chkStop_Auto_Refresh.Text = "Data Will Be Refresh in " + m_iCountTime.ToString("D2") + " s.";
                if (m_iCountTime == 0)
                {
                    Init_Data();
                    grdData.RefreshDataSource();
                    m_iCountTime = 30;
                }
                else
                {
                    m_iCountTime--;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void grvData_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                

                m_objSelectedAppointment = (CAppointments)grvData.GetRow(grvData.FocusedRowHandle);
                Load_Task_Asset(m_objSelectedAppointment.Auto_ID);
                grdAsset.DataSource = m_lsAsset;

                //set loại thông tin xử lý:
                if (m_objSelectedAppointment.Status == 0)
                {
                    btnStart.Enabled = true;
                    btnDone_And_Send_To_Artlead.Enabled = false;
                    btnDone_And_Send_To_Technical.Enabled = false;
                }
                if (m_objSelectedAppointment.Status == 1)
                {
                    btnStart.Enabled = false;
                    btnDone_And_Send_To_Artlead.Enabled = true;
                    btnDone_And_Send_To_Technical.Enabled = true;
                }
                if (m_objSelectedAppointment.Status == 2)
                {
                    btnStart.Enabled = false;
                    btnDone_And_Send_To_Artlead.Enabled = false;
                    btnDone_And_Send_To_Technical.Enabled = false;
                }

                if (m_objSelectedAppointment.Status == 3)
                {
                    btnStart.Enabled = false;
                    btnDone_And_Send_To_Artlead.Enabled = false;
                    btnDone_And_Send_To_Technical.Enabled = false;
                }

                if (m_objSelectedAppointment.Status == 4)
                {
                    btnStart.Enabled = false;
                    btnDone_And_Send_To_Artlead.Enabled = false;
                    btnDone_And_Send_To_Technical.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void chkStop_Auto_Refresh_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (chkStop_Auto_Refresh.Checked == true)
                {
                    TimerRefreshData.Stop();
                }
                else
                {
                    TimerRefreshData.Start();
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                Init_Data();
                grdData.RefreshDataSource();
                m_iCountTime = 30;
                chkStop_Auto_Refresh.Text = "Data Will Be Refresh in " + m_iCountTime.ToString("D2") + " s.";
                frmMain v_frm = (frmMain)this.Parent.Parent.Parent.Parent;
                v_frm.show_balloon("You have " + m_lsAppointment.Count + " tasks to do.");
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                m_objSelectedAppointment.Status = 1;
                m_objSelectedAppointment.Actual_Start_Date = DateTime.Now;
                CAppointments_Controller v_CtrlApp = new CAppointments_Controller();
                v_CtrlApp.Update_Appointments(m_objSelectedAppointment);
                grdData.RefreshDataSource();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnDone_And_Send_To_Artlead_Click(object sender, EventArgs e)
        {
            try
            {
                m_objSelectedAppointment.Status = CUtility.Convert_To_Int32(ETasks_Status.Wait_Art_Lead);
                m_objSelectedAppointment.Actual_End_Date = DateTime.Now;
                m_objSelectedAppointment.Percent_Complete = 100;
                CAppointments_Controller v_CtrlApp = new CAppointments_Controller();
                m_lsAppointment.Remove(m_objSelectedAppointment);

                v_CtrlApp.Update_Appointments(m_objSelectedAppointment);
                grdData.RefreshDataSource();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnDone_And_Send_To_Technical_Click(object sender, EventArgs e)
        {
            try
            {
                m_objSelectedAppointment.Status = CUtility.Convert_To_Int32(ETasks_Status.Wait_Technicial);
                m_objSelectedAppointment.Actual_End_Date = DateTime.Now;
                m_objSelectedAppointment.Percent_Complete = 100;
                CAppointments_Controller v_CtrlApp = new CAppointments_Controller();
                m_lsAppointment.Remove(m_objSelectedAppointment);

                v_CtrlApp.Update_Appointments(m_objSelectedAppointment);
                grdData.RefreshDataSource();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }
    }
}
